<!-- 渠道管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyName" label-width="68px" >
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区：" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入所在地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="level">
                    <el-select v-model="queryParams.level" placeholder="请选择客户级别" style="width: 205px;">
                        <el-option label="A" value="A"></el-option>
                        <el-option label="B" value="B"></el-option>
                        <el-option label="C" value="C"></el-option>
                        <el-option label="D" value="D"></el-option>
                    </el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-" type="success" size="mini" @click="add" >添加</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="所在地区" align="center" prop="area" />
      <el-table-column label="级别" align="center" prop="level" />
      <el-table-column label="业务联系人" align="center" prop="transactionPerson" />
      <el-table-column label="联系方式" align="center" prop="transactionPhone" />

      <el-table-column label="签约时间" align="center" prop="signingTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template  slot-scope="scope">
          <span style="color: red;" v-if="scope.row.status == '0'">禁用</span>
          <span  style="color: green;" v-if="scope.row.status == '1'">开通</span>
        </template>
      </el-table-column>
     
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="big"
            type="text"
            @click="update(scope.row)"
          >编辑</el-button>
          <el-button
            size="big"
            type="text"
            @click="opens(scope.row)"
          >开通</el-button>
          
          <el-button
            size="big"
            type="text"
            @click="disable(scope.row)"
          >禁用</el-button>
          
           <el-button
            size="big"
            type="text"
            @click="details(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
      <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        :page-sizes="[5,10,15,20]"
        @pagination="getList" />
  </div>
</template>

<script>
import {listChannel,updateStatus} from "@/api/credit/crechannel.js";

export default {
  name: "Product",
  data() {
    return {
      stat:'',
      //还款方式
      credit:'',
      //贷款期限
      term:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用 
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道表格数据
      channelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: '',
        area: '',
        level:'',
       
      },
      // 表单参数
      form: {
        companyName:null,
        area: null,
        level: null,
        transactionPerson: null,
        transactionPhone: null,
        signingTime: null,
        status: null,

      },
      // 表单校验
      rules: {
      //   applymoney: [
      //   { required: true, message: "用户名称不能为空", trigger: "blur" },
      //   {  message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
      // ],
      }
    };
  },
  created() {
    this.getList();
   
  },
  methods: {
    /** 查询渠道列表 */
    getList() { 
      this.loading = false;
      listChannel(this.queryParams).then(response => {
        console.log(response.rows);
          this.channelList = response.rows;
          this.total = response.total;
          this.loading = false;
      });
    },
   
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        escrow: null,
        monthRate: null,
        consultingRate: 0,
        marginRate: 0,
        paymentMethod: null,
        loanTerm: null,
       
      };
      this.resetForm("form");
    },
    search(){
      listChannel(this.queryParams).then(res=>{
        this.channelList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.search();
      
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    
    /** 开通按钮操作 */
    opens(row){
      console.log(row);
      this.$confirm('是否为合作渠道开通服务', '确认提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        // row.status='1'
        this.stat='1'
        console.log(row);
        console.log(this.stat);
        updateStatus(row.id,this.stat).then(res=>{
          this.getList();
        }).catch();
        this.$message({
          type: 'success',
          message: '服务开通成功'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });          
      });
    },
    /** 禁用按钮操作 */
    disable(row){
      console.log(row);
      this.$confirm('是否禁用合作渠道服务', '确认提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        // row.status='1'
        this.stat='0'
        console.log(row);
        console.log(this.stat);
        updateStatus(row.id,this.stat).then(res=>{
          this.getList();
        }).catch();
        this.$message({
          type: 'success',
          message: '合作渠道已被禁用'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });          
      });
    },
    /** 查看详情按钮操作 */
    details(row){
      this.$router.push({name:'/cre_channel',params:{detailsSelect:row.id}})
    },
    /** 添加按钮 */
    add(){
      this.$router.push({name:'/cre_channel',query:{title:'添加渠道'}})
    },
    /** 编辑按钮 */
    update(data){
      this.$router.push({name:'/cre_channel',params:{data:data.id}})
    },
      /** 提交按钮 */
      submitForm() {
      this.$refs["form"].validate(valid => {
        // if (valid) {
        //   if (this.form.id != null) {
        //     updateProducts(this.form).then(response => {
        //       this.$modal.msgSuccess("修改成功");
        //       this.open = false;
        //       this.getList();
        //     });
        //   } else {
        //     addProducts(this.form).then(response => {
        //       this.$modal.msgSuccess("新增成功");
        //       this.open = false;
        //       this.getList();
        //     });
        //   }
        // }
        //console.log(this.form.user_id);
        console.log(this.form);
        this.form.productId=this.form.id;
        // 购买
        verifInfo(this.form).then(res=>{
          console.log(res);
          if(res.code==200){
            console.log(111);
            this.$modal.msgSuccess(res.msg);
              this.open = false;
          }else if(res.code==5000){
            debugger;
            this.$modal.msgSuccess(res.msg);
            this.$router.push({ path: '/credit_user/info/user/profile' })
            
          }
        }).catch(()=>{
          //this.$router.push({ path: '/credit_user/info/user/profile' })
        });
      });
    },
   
  
   
  }
};
</script>